package com.baizhi.config;
import com.baizhi.util.AuthorRealm;
import com.baizhi.util.DataResourceRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: renpu
 * @Date: 2019/3/14 19:08
 * @Description:
 */
@Configuration
public class ShiroConfiguration {


    //自定义数据源，从数据库查询的数据封装到DataResourceRealm对象里；
    @Bean
    public DataResourceRealm getDataResourceRealm(){
        return new DataResourceRealm();
    }

    //获取进行授权的数据源
    @Bean
    public AuthorRealm getAuthorRealm(){
        return new AuthorRealm();
    }



    //获取shiro的核心对象：安全管理器；
    @Bean
    public DefaultWebSecurityManager getSecurityManager(AuthorRealm realm){


        //获取安全管理器对象
        DefaultWebSecurityManager securityManager=new DefaultWebSecurityManager();

        //自定义数据源与安全管理器建立联系；
        securityManager.setRealm(realm);
        return securityManager;
    }

 /*   @Bean
    public SecurityManager getSecurityManager(DataSourceRealm dataSourceRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(dataSourceRealm);
        return securityManager;
    }*/


    @Bean
    public ShiroFilterFactoryBean ShiroFilterFactoryBean(SecurityManager securityManager){


        //获取shiro框架内封装的过滤器
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //创建Map对象；
        Map map = new HashMap<>();

        //设置拦截规律；anon匿名可以访问，不需要登录就可以访问；
        //authc必须登录才可以进行访问；
        map.put("/login.jsp","anon");
        map.put("/Admin/login","anon");
        map.put("/redis/redisTest","anon");
        map.put("/img/favicon.ico","anon");
        map.put("/css/common.css","anon");
        map.put("/css/login.css","anon");
        map.put("/script/jquery.js","anon");
        map.put("/script/common.js","anon");

        map.put("/**","authc");

        //过滤将所有拦截的规律联系在一起
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        //将过滤器交给安全管理器进行管理；
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        return shiroFilterFactoryBean;
    }
}
